﻿Imports System.Security.Principal
Imports TBCC.Utility

Public Class User

    Private Shared _instance As User

    Public Shared ReadOnly Property Instance() As User
        Get
            If _instance Is Nothing Then
                _instance = New User()
            End If
            Return _instance
        End Get
    End Property

    Private _uID As String
    Public ReadOnly Property uID() As String
        Get
            Return _uID
        End Get
    End Property

    Private _uName As String
    Public ReadOnly Property uName() As String
        Get
            Return _uName
        End Get
    End Property

    Public Function Login(ByVal uid As String, ByVal pwd As String) As Boolean
        Dim strSql As String
        strSql = "Select Count(*) From tbccUser where uID=?p0 and uPassword=?p1 and uEnable=1"
        Dim p(1) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New MySql.Data.MySqlClient.MySqlParameter()
        Next
        p(0).ParameterName = "p0" : p(0).Value = uid
        p(1).ParameterName = "p1" : p(1).Value = UserClass.GetMD5(pwd)
        Dim obj As Object
        Try

            obj = SqlHelper.ExecuteScalar(strSql, p)
            If obj <> 1 Then
                Return False
            End If
        Catch ex As Exception
            Throw ex

        End Try




        '读取权限
        strSql = "Select b.roleID,b.rolename,c.uID,c.uName From tbccUserRole a,tbccRoles b,tbccUser c where a.uId=?p3 and a.roleID=b.roleID and c.uID=a.uID"

        Dim p2(0) As MySql.Data.MySqlClient.MySqlParameter
        p2(0) = New MySql.Data.MySqlClient.MySqlParameter()
        p2(0).ParameterName = "p3" : p2(0).Value = uid

        Dim ds As DataSet
        ds = SqlHelper.ExecuteDataset(strSql, p2)

        Dim strRoles As String = ""
        For Each r As DataRow In ds.Tables(0).Rows
            strRoles &= Convert.ToString(r("rolename")).Trim() & ","
            Me._uID = r("uID")
            Me._uName = r("uName")
        Next

        If strRoles.Length > 0 Then
            strRoles = strRoles.TrimEnd(",")
        End If

        Dim roles() As String = strRoles.Split(",")

        Dim id As New GenericIdentity(uid)
        Dim gp As New GenericPrincipal(id, roles)
        My.User.CurrentPrincipal = gp

        If My.User.CurrentPrincipal.IsInRole(RoleClass.GetRolename(RoleType.Admin)) Then
        End If

        Return True
    End Function

    Public Sub Logout()
        _uID = ""
        _uName = ""
    End Sub

    Public Function CheckOldPassword(ByVal Pwd As String) As Boolean
        Dim strSql As String
        strSql = "Select Count(*) From tbccUser where uID=?p0 and uPassword=?p1 and uEnable=1"
        Dim p(1) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New MySql.Data.MySqlClient.MySqlParameter()
        Next
        p(0).ParameterName = "p0" : p(0).Value = _uID
        p(1).ParameterName = "p1" : p(1).Value = UserClass.GetMD5(Pwd)

        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, p)

        If obj <> 1 Then
            Return False
        End If

        Return True
    End Function

    ''' <summary>
    ''' 修改密码
    ''' </summary>
    ''' <param name="oldPassword"></param>
    ''' <param name="newPassword"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ChangePassword(ByVal oldPassword As String, ByVal newPassword As String) As Boolean
        Dim strSql As String
        strSql = "Update tbccUser Set uPassword=?p0 where uID=?p1 And uPassword=?p2"
        Dim p(2) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New MySql.Data.MySqlClient.MySqlParameter
        Next

        p(0).ParameterName = "p0" : p(0).Value = UserClass.GetMD5(newPassword)
        p(1).ParameterName = "p1" : p(1).Value = Me._uID
        p(2).ParameterName = "p2" : p(2).Value = UserClass.GetMD5(oldPassword)

        Dim result As Integer
        result = SqlHelper.ExecuteNonQuery(strSql, p)

        Return 1 = result
    End Function
    Public Function GetRole() As GenericPrincipal
        Return My.User.CurrentPrincipal
    End Function
End Class
